import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Query,
  BadRequestException,
  Delete,
} from '@nestjs/common';
import { CateService } from './cate.service';
import { CreateCateDto } from './dto/create-cate.dto';
import { UpdateCateDto } from './dto/update-cate.dto';
import { CateEntity } from './entities/cate.entity';

@Controller('cate')
export class CateController {
  constructor(private readonly cateService: CateService) {}

  @Post()
  create(@Body() createCateDto: CreateCateDto) {
    return this.cateService.create(createCateDto);
  }

  //获取分类列表
  @Get('lists')
  async findAll(
    @Query('pageNum') pageNum = 1,
    @Query('pageSize') pageSize = 10,
  ): Promise<{
    code: number;
    data: CateEntity[];
    total: number;
  }> {
    return this.cateService.findAll(pageNum, pageSize);
  }

  //获取分类详情
  @Get('list') //http://localhost:3000/api/text/list?bid=1
  async findOne(
    @Query('cid') cid: number,
    @Query('pageNum') pageNum = 1,
    @Query('pageSize') pageSize = 10,
  ): Promise<{
    code: number;
    data: CateEntity[];
    total: number;
  }> {
    if (!cid) {
      throw new BadRequestException('缺少必填参数: cid');
    }
    return this.cateService.findOne(cid, pageNum, pageSize);
  }

  // @Get(':id')
  // findOne(@Param('id') id: string) {
  //   return this.cateService.findOne(+id);
  // }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateCateDto: UpdateCateDto) {
    return this.cateService.update(+id, updateCateDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.cateService.remove(+id);
  }
}
